﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.LiveModels.Models;
using Guline.Web.LiveModels.DataContext;

namespace Guline.Web.Livechat.Controllers
{
    public class MessageController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Message/
       public ActionResult jsonList()
        {


            var list = (from n in db.Messages select new {n.User.username,n.content,n.daytime,n.ID });
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {

            return View();

        }

        //
        // GET: /Message/Details/5

        public ActionResult Details(int id = 0)
        {
            Message message = db.Messages.Find(id);
            if (message == null)
            {
                return HttpNotFound();
            }
            return View(message);
        }

        //
        // GET: /Message/Create

        public ActionResult Create()
        {
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username");
            return View();
        }

        //
        // POST: /Message/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Message message)
        {
            if (ModelState.IsValid)
            {
                message.daytime = DateTime.Now;
                db.Messages.Add(message);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.user_ID = new SelectList(db.Users, "ID", "username", message.user_ID);
            return View(message);
        }

        //
        // GET: /Message/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Message message = db.Messages.Find(id);
            if (message == null)
            {
                return HttpNotFound();
            }
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username", message.user_ID);
            return View(message);
        }

        //
        // POST: /Message/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Message message)
        {
            if (ModelState.IsValid)
            {
                message.daytime = DateTime.Now;
                db.Entry(message).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username", message.user_ID);
            return View(message);
        }

        //
        // GET: /Message/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Message message = db.Messages.Find(id);
            if (message == null)
            {
                return HttpNotFound();
            }
            return View(message);
        }

        //
        // POST: /Message/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Message message = db.Messages.Find(id);
            db.Messages.Remove(message);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             Message message = db.Messages.Find(id);
			 if (message == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.Messages.Remove(message);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}